package com.wxy.demo.controller;

import com.wxy.demo.task.PrintTimeTask;
import com.wxy.demo.util.JobUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @Autowired
    private Scheduler scheduler;

    /**
     * 创建触发器和任务
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/start")
    public String start() throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey("triggerKey");
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if (trigger == null) {
            // 1.创建数据集
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("name", "张三");
            // 2.创建定时任务
            JobKey jobKey = JobKey.jobKey("jobKey");
            JobDetail jobDetail = JobBuilder.newJob(PrintTimeTask.class).withIdentity(jobKey)
                    .setJobData(jobDataMap).build();
            // 3.创建cron表达式
            CronScheduleBuilder schBuilder = CronScheduleBuilder.cronSchedule("0/20 * * * * ?")
                    .withMisfireHandlingInstructionDoNothing();
            // 4.创建触发器
            trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey)
                    .withSchedule(schBuilder)
                    .build();
            // 5.加入调度器
            scheduler.scheduleJob(jobDetail, trigger);
            return "success:" + JobUtils.now();
        } else {
            return "当前job已存在";
        }
    }

    /**
     * 更新触发器
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/update")
    public String update() throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey("triggerKey");
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if (trigger != null) {
            // 1.创建cron表达式
            CronScheduleBuilder schBuilder = CronScheduleBuilder.cronSchedule("0/30 * * * * ?")
                    .withMisfireHandlingInstructionDoNothing();
            // 2.创建触发器
            trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey)
                    .withSchedule(schBuilder)
                    .build();
            scheduler.rescheduleJob(triggerKey, trigger);
            return "success:" + JobUtils.now();
        } else {
            return "当前job不存在";
        }
    }

    /**
     * 暂停触发器
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/trigger/stop")
    public String stop() throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey("triggerKey");
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if (trigger != null) {
            scheduler.pauseTrigger(triggerKey);
            return "success:" + JobUtils.now();
        } else {
            return "当前job不存在";
        }
    }

    /**
     * 暂停任务
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/job/stop")
    public String stop1() throws SchedulerException {
        JobKey jobKey = JobKey.jobKey("jobKey");
        scheduler.pauseJob(jobKey);
        return "success:" + JobUtils.now();
    }

    /**
     * 恢复触发器
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/trigger/resume")
    public String resume() throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey("triggerKey");
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        if (trigger != null) {
            scheduler.resumeTrigger(triggerKey);
            return "success:" + JobUtils.now();
        } else {
            return "当前job不存在";
        }
    }

    /**
     * 恢复任务
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/job/resume")
    public String resume1() throws SchedulerException {
        JobKey jobKey = JobKey.jobKey("jobKey");
        scheduler.resumeJob(jobKey);
        return "success:" + JobUtils.now();
    }

    /**
     * 删除任务
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/delete")
    public String restart() throws SchedulerException {
        JobKey jobKey = JobKey.jobKey("jobKey");
        scheduler.deleteJob(jobKey);
        return "success:" + JobUtils.now();
    }

    /**
     * 立即执行
     *
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/run")
    public String run() throws SchedulerException {
        JobKey jobKey = JobKey.jobKey("jobKey");
        scheduler.triggerJob(jobKey);
        return "success:" + JobUtils.now();
    }
}

